package app;
import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.Topic;
import javax.naming.Context;
import javax.naming.InitialContext;

import model.Message;


/** * 
 * @author minja
 *
 */
public class JMSTopic {
	public JMSTopic() {
		try {
			final Properties env = new Properties();
			env.put(Context.INITIAL_CONTEXT_FACTORY,
					"org.jboss.naming.remote.client.InitialContextFactory");
			//ako bude problema, promeniti u liniji ispod localhost na ip adresu servera
			env.put(Context.PROVIDER_URL, "remote://localhost:4447");
			env.put(Context.SECURITY_PRINCIPAL, "guest");
			env.put(Context.SECURITY_CREDENTIALS, "guestguest");
			Context context = new InitialContext(env);
			ConnectionFactory cf = (ConnectionFactory) context
					.lookup("jms/RemoteConnectionFactory");
			final Topic topic = (Topic) context
					.lookup("jms/topic/mojTopic");
			context.close();
			Connection connection = cf.createConnection("guest", "guestguest");
			final Session session = connection.createSession(false,
					Session.AUTO_ACKNOWLEDGE);

			connection.start();
			
			// create and publish a message
			ObjectMessage msg = session.createObjectMessage();
			msg.setObject(new Message(null,null,null,"hocu 9000+ banana"));
			MessageProducer producer = session.createProducer(topic);
			producer.send(msg);
			System.out.println("Message sent.");
			producer.close();
			connection.stop();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new JMSTopic();
	}
}
